﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.Windows.Automation;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Configuration;

namespace RemoteDesktopsTest
{
  /// <summary>
  /// Summary description for UnitTest1
  /// </summary>
  [TestClass]
  public class RemoteDesktopsTest
  {
    //private Process _userAppProcess;
    //private AutomationElement _userApplicationElement;

    public RemoteDesktopsTest()
    {}

    //[TestInitialize]
    //public void SetUp()
    //{
    //  string path = ConfigurationManager.AppSettings["exepath"];
    //  string exePath = String.Format("{0}\\{1}", path, ConfigurationManager.AppSettings["appname"]);
    //  Thread appThread = new Thread(delegate()
    //  {
    //    _userAppProcess = new Process();
    //    _userAppProcess.StartInfo.FileName = exePath;
    //    _userAppProcess.StartInfo.WorkingDirectory = path;
    //    _userAppProcess.StartInfo.UseShellExecute = false;
    //    _userAppProcess.Start();
    //  });
    //  appThread.SetApartmentState(ApartmentState.STA);
    //  appThread.Start();

    //  WaitForApplication();
    //}

    ///// <summary>
    ///// 
    ///// </summary>
    //private void WaitForApplication()
    //{
    //  AutomationElement ae = AutomationElement.RootElement;
    //  if (ae == null)      
    //    throw new Exception("Unable to get Desktop");      
    //  this._userApplicationElement = null;
    //  do
    //  {
    //    this._userApplicationElement = AutomationElementSearch.FindElementById(ae, ConfigurationManager.AppSettings["windowname"]);
    //    Thread.Sleep(200);
    //  } while ((this._userApplicationElement == null || this._userApplicationElement.Current.IsOffscreen));

    //}

    //[TestCleanup]
    //public void CleanUp()
    //{
    //  try
    //  {
    //    // Tell the application's main window to close.
    //    WindowPattern window = this._userApplicationElement.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
    //    window.Close();
    //    if (!this._userAppProcess.WaitForExit(3000))
    //    {
    //      // We waited 3 seconds for the User Application to close on its own.  
    //      // Send a close request again through the process class.
    //      this._userAppProcess.CloseMainWindow();
    //    }

    //    // All done trying to close the window, terminate the process
    //    this._userAppProcess.Close();
    //    this._userAppProcess = null;
    //  }
    //  catch
    //  { }
    //}

    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    //
    // You can use the following additional attributes as you write your tests:
    //
    // Use ClassInitialize to run code before running the first test in the class
    // [ClassInitialize()]
    // public static void MyClassInitialize(TestContext testContext) { }
    //
    // Use ClassCleanup to run code after all tests in a class have run
    // [ClassCleanup()]
    // public static void MyClassCleanup() { }
    //
    // Use TestInitialize to run code before running each test 
    // [TestInitialize()]
    // public void MyTestInitialize() { }
    //
    // Use TestCleanup to run code after each test has run
    // [TestCleanup()]
    // public void MyTestCleanup() { }
    //
    #endregion

    [TestMethod]
    public void AddDesktop()
    {

//      AutomationElement element = AutomationElementSearch.FindElementById(this._userApplicationElement, "MainMenu");
        

//      //get the children
//      AutomationElementCollection aeCol = AutomationElementSearch.FindElementsByType(element, ControlType.MenuItem);

////      PropertyCondition findWindow = new PropertyCondition(AutomationElement.NameProperty, this.Title);
////AutomationElement window = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, findWindow);
////PropertyCondition findButton = new PropertyCondition(AutomationElement.AutomationIdProperty,"Button");
////AutomationElement button = window.FindFirst(TreeScope.Descendants, findButton); // this returns null :(
////InvokePattern pattern = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
////pattern.Invoke()

//      foreach (AutomationElement aeItem in aeCol)
//      {
        

//        object pattern = null;
//        bool test = aeItem.TryGetCurrentPattern(InvokePattern.Pattern, out pattern);

//        //pattern.Invoke();
//        Console.WriteLine(aeItem.Current.Name);
//      }
    }
  }
}
